<?php
// 如果这个整屋在初审中的话，将单个报名作品同步到初审
namespace Beerus\Modules\DesignerCompetition2024\Actions;

use Doraemon\model\ReDesignerCompetition\Audit\Audit2022\DcAuditReviewer;
use Doraemon\model\ReDesignerCompetition\Audit\DcAuditFirstResultHelper;
use Doraemon\model\ReDesignerCompetition\Audit\DcAuditFirstWorkHelper;
use Doraemon\model\ReDesignerCompetition\Sign\DcUserSignHelper;
use Doraemon\pockets\constInterface\Designer\DesignerCompetition2022;
use Doraemon\service\DesignerCompetition\DcCommon;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;

class SyncFirstAudit extends RowAction implements DesignerCompetition2024 {
    public $name = '同步初审';

    public function handle($iSignId) {
        $aSignInfo = (DcUserSignHelper::getInstance())->getSignItemByIdWithDataStatus($iSignId);
        $aAwardIds = explode(',', $aSignInfo['award_ids']);
        if ($aSignInfo['data_status'] == DcUserSignHelper::STATUS_DELETE) {
            // 如果是删除状态，则从初审移除
            // 先移除初审结果表中的数据、再移除初审池中的数据
            $aCondition = [
                'sign_id' => $iSignId
            ];
            (DcAuditFirstResultHelper::getInstance())->batchUpdateDataStatus2DelByCondition($aCondition);
            (DcAuditFirstWorkHelper::getInstance())->batchUpdateDataStatus2DelByCondition($aCondition);
        } else {
            // 如果是正常状态，则加入初审
            // 如果是新增的报名作品，则直接加入作品池，然后加入评审表
            // 如果是修改的报名作品，则更新作品池和评审表中的数据，多退少补
            // 判断是否是新增的作品
//            $aWorkItem = (DcAuditFirstWorkHelper::getInstance())->getItemBySignId($iSignId);
            $aReviewerList = DcAuditReviewer::select('*', ['audit_step' => 1]);
            // 先从作品池和评审表中移除该作品相关数据
            // 然后统一新增
            (DcAuditFirstWorkHelper::getInstance())->batchUpdateDataStatus2DelByCondition(['sign_id' => $iSignId]);
            (DcAuditFirstResultHelper::getInstance())->batchUpdateDataStatus2DelByCondition(['sign_id' => $iSignId]);
            foreach ($aAwardIds as $iAwardId) {
                if (in_array($iAwardId, array_merge(self::MAIN_AWARD_ID_LIST, self::SINGLE_AWARD_ID_LIST))) {
                    $aWorkData = [
                        'sign_id' => $iSignId,
                        'award_id' => $iAwardId,
                        'uid' => $aSignInfo['uid'],
                        'user_type' => $aSignInfo['user_type'],
                        'sign_award_ids' => $aSignInfo['award_ids'],
                        'audit_award_ids' => $aSignInfo['award_ids']
                    ];
                    (DcAuditFirstWorkHelper::getInstance())->addWorkWithData($aWorkData);
                    // 再载入评审表
                    DcCommon::import2FirstResult($aWorkData, $aReviewerList);
                }
            }
        }
        return $this->response()->success('同步成功')->refresh();
    }

    public function retrieveModel(Request $request) {
        if (!$key = $request->get('_key')) {
            return false;
        }
        return $key;
    }

    public function dialog() {
        $this->confirm('确定要同步该作品到初审？');
    }
}
